﻿<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <link rel="Shortcut Icon" href="../images/aecc.ico" type="image/x-icon" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="../StaticFiles/element-ui/element-ui.css">
  <link rel="stylesheet" href="../StaticFiles/nprogress/nprogress.min.css">
  <link rel="stylesheet" href="../styles/vue-page-common.css">
  <script src="../Scripts/vue.min.js"></script>
  <script src="../StaticFiles/element-ui/element-ui.js"></script>
  <script src="../Scripts/axios.min.js"></script>
  <script src="../StaticFiles/nprogress/nprogress.min.js"></script>
  <script src="../Scripts/dayjs.min.js"></script>
  <script src="../Scripts/axios.config.js"></script>
  <script src="../Scripts/crypto-js.js"></script>
  <script src="../Scripts/secret.js"></script>
  <script src="../Scripts/xlsx.full.min.js"></script>
  <title>发动机工时统计报表</title>
  <style>
    .card {
      border: 1px solid #409EFF;
      border-radius: 3px;
      margin: 3px;
      padding: 4px 7px;
    }
  </style>
</head>

<body>
  <div id="app" v-cloak v-on:keyup.enter="getData">
    <!-- 报表标题 -->
    <vue2-report-header :title="queryInfo.factoryname+'发动机工时统计报表'"></vue2-report-header>
    <div style="padding: 10px;" id="queryZone">
      <!-- 查询条件区 -->
      <el-form :inline="true" :model="queryInfo" ref="queryInfoRef" :label-position="'left'" :rules="rules"
        :disabled="formDisabled">
        <el-form-item label="检验时间:" prop="checkdate">
          <el-date-picker v-model="queryInfo.checkdate" type="daterange" range-separator="至" value-format="yyyy-MM-dd"
            start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions"></el-date-picker>
        </el-form-item>

        <el-form-item prop='engineno' label="发动机号:">
          <el-autocomplete v-model="queryInfo.engineno" placeholder="请输入发动机号" :trigger-on-focus="false"
            :fetch-suggestions="getEngineNoAsync" :value-key="'engineno'" :debounce="500" style="width:200px;">
          </el-autocomplete>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="small" @click="getData">查询</el-button>
          <el-button type="primary" icon="el-icon-download" size="small" @click="getExportExcel">导出</el-button>
        </el-form-item>
      </el-form>

      <!-- 表格显示区 -->
      <el-table v-loading="loading" v-loading.fullscreen.lock="loading" :element-loading-text="loadingText"
        element-loading-background="rgba(0,0,0,0.8)" :data="dataList" stripe border>
        <el-table-column type="index" width="50px"></el-table-column>
        <el-table-column label="发动机号" prop="machineno" width="200" show-overflow-tooltip></el-table-column>
        <el-table-column label="初装各组工时" prop="czteamworktime" width="200" show-overflow-tooltip>
          <template slot-scope="scope" v-if="scope.row.czteamworktime">
            <div v-for="i in scope.row.czteamworktime" :label="i.teamname" :key="i.teamname" class="card">
              {{i.teamname}}: {{i.worktime}}小时
            </div>
          </template>
        </el-table-column>
        <el-table-column label="初装总工时" prop="czworktimetotal" width="120" show-overflow-tooltip>
          <template slot-scope="scope">{{scope.row.czworktimetotal}}小时</template>
        </el-table-column>
        <el-table-column label="检装各组工时" prop="jzteamworktime" width="200" show-overflow-tooltip>
          <template slot-scope="scope" v-if="scope.row.jzteamworktime">
            <div v-for="i in scope.row.jzteamworktime" :label="i.teamname" :key="i.teamname" class="card">
              {{i.teamname}}: {{i.worktime}}小时
            </div>
          </template>
        </el-table-column>
        <el-table-column label="检装总工时" prop="jzworktimetotal" width="120" show-overflow-tooltip>
          <template slot-scope="scope">{{scope.row.jzworktimetotal}}小时</template>
        </el-table-column>
        <el-table-column label="工厂试车各组工时" prop="gcscteamworktime" width="200" show-overflow-tooltip>
          <template slot-scope="scope" v-if="scope.row.gcscteamworktime">
            <div v-for="i in scope.row.gcscteamworktime" :label="i.teamname" :key="i.teamname" class="card">
              {{i.teamname}}: {{i.worktime}}小时
            </div>
          </template>
        </el-table-column>
        <el-table-column label="工厂试车总工时" prop="gcscworktimetotal" width="120" show-overflow-tooltip>
          <template slot-scope="scope">{{scope.row.gcscworktimetotal}}小时</template>
        </el-table-column>
        <el-table-column label="检验试车各组工时" prop="jyscteamworktime" width="200" show-overflow-tooltip>
          <template slot-scope="scope" v-if="scope.row.jyscteamworktime">
            <div v-for="i in scope.row.jyscteamworktime" :label="i.teamname" :key="i.teamname" class="card">
              {{i.teamname}}: {{i.worktime}}小时
            </div>
          </template>
        </el-table-column>
        <el-table-column label="检验试车总工时" prop="jyscworktimetotal" width="120" show-overflow-tooltip>
          <template slot-scope="scope">{{scope.row.jyscworktimetotal}}小时</template>
        </el-table-column>
        <el-table-column label="封包组工时" prop="fbztotal" width="120" show-overflow-tooltip>
          <template slot-scope="scope">
            {{scope.row.fbztotal}}小时
          </template>
        </el-table-column>
        <el-table-column label="汇总工时" prop="worktime" width="120" show-overflow-tooltip>
          <template slot-scope="scope">{{scope.row.worktime}}小时</template>
        </el-table-column>
      </el-table>
      <!-- 回到顶部 -->
      <el-backtop></el-backtop>
    </div>
  </div>

  <script type="module">
    import { getParams, fetchSuggestion, exportExcelData } from '../Scripts/hooks/useCommonMethod.js'
    import { getFactoryInfo } from '../Scripts/hooks/fetchAPI.js'
    import { prevWeek, thisWeek, nextWeek, prevMonth, thisMonth, nextMonth } from '../Scripts/hooks/useDateShortCut.js'
    import Vue2ReportHeader from '../Scripts/components/Vue2ReportHeader.js'
    const app = new Vue({
      el: '#app',
      components: { Vue2ReportHeader },
      data: {
        total: 0,
        loading: false,
        loadingText: '数据查询中...',
        queryInfo: {
          factoryname: '',
          factoryid: '',
          engineno: '',//发动机号
          checkdate: ''//检验时间
        },
        dataList: [],
        userInfo: {},
        formDisabled: false,
        rules: { checkdate: [{ required: true, message: '检验时间不能为空', trigger: 'change' }] },
        pickerOptions: { shortcuts: [prevWeek, thisWeek, nextWeek, prevMonth, thisMonth, nextMonth] }
      },
      methods: {
        // 查询发动机号
        getEngineNoAsync(value, callback) {
          fetchSuggestion({ actionName: 'getEngineNoAsync', name: 'engineno', value, callback })
        },
        // 查询
        async getData() {
          this.$refs['queryInfoRef'].validate(async (valid) => {
            if (!valid) {
              return this.$message.error('必填项不能为空');
            } else {
              this.loading = true;
              this.loadingText = '数据加载中...';
              const { data: { data, meta, total } } = await axios.post('../handler/report/ZSEngineWorktimeReport.ashx?action=getData', {
                type: 'getData',
                ...this.queryInfo
              })
              this.loading = false;
              if (meta.status === 500) { return this.$message.error(meta.msg); }
              this.dataList = data;
            }
          })
        },
        // 导出
        async getExportExcel() {
          this.$refs['queryInfoRef'].validate(async (valid) => {
            if (!valid) {
              return this.$message.error('必填项不能为空');
            } else {
              this.loading = true;
              this.loadingText = '导出中...';
              const { data: res } = await axios.post('../handler/report/ZSEngineWorktimeReport.ashx?action=getData', {
                type: 'getData',
                ...this.queryInfo
              })
              this.loading = false;
              if (res.meta.status === 500) {
                return this.$message.error(`导出时出错！${res.meta.msg}`);
              }
              this.dataList = res.data;
              let data = [];
              res.data.forEach(item => {
                let czteamworktime = '',//初装各组工时
                  jzteamworktime = '',//检装各组工时
                  gcscteamworktime = '',//工厂试车各组工时
                  jyscteamworktime = '';//检验试车总工时
                item.czteamworktime.forEach(i => czteamworktime += `${i.teamname}:${i.worktime};`);
                item.jzteamworktime.forEach(i => jzteamworktime += `${i.teamname}:${i.worktime};`);
                item.gcscteamworktime.forEach(i => gcscteamworktime += `${i.teamname}:${i.worktime};`);
                item.jyscteamworktime.forEach(i => jyscteamworktime += `${i.teamname}:${i.worktime};`);

                data.push({
                  '发动机号': item.machineno,
                  '初装各组工时': czteamworktime,
                  '初装总工时': item.czworktimetotal,
                  '检装各组工时': jzteamworktime,
                  '检装总工时': item.jzworktimetotal,
                  '工厂试车各组工时': gcscteamworktime,
                  '工厂试车总工时': item.gcscworktimetotal,
                  '检验试车各组工时': jyscteamworktime,
                  '检验试车总工时': item.jyscworktimetotal,
                  '封包组工时': item.fbztotal,
                  '汇总工时': item.worktime,
                });
              })
              // 1.创建工作簿
              const workbook = XLSX.utils.book_new();
              // 2.创建工作表
              const worksheet = XLSX.utils.json_to_sheet(data);
              worksheet['!cols'] = [
                { wpx: 100 },
                { wpx: 200 },
                { wpx: 100 },
                { wpx: 200 },
                { wpx: 100 },
                { wpx: 200 },
                { wpx: 100 },
                { wpx: 100 },
                { wpx: 100 },
                { wpx: 100 },
                { wpx: 100 }
              ];
              // 3.将工作表添加到工作簿
              XLSX.utils.book_append_sheet(workbook, worksheet, "Sheet1");
              // 4.将工作簿导出为Excel文件
              XLSX.writeFile(workbook, `发动机工时统计报表${dayjs().format('YYYY-MM-DD HH:mm:ss')}.xlsx`);
              this.$message.success('导出成功');
            }
          })
        }
      },
      async mounted() {
        const startOfMonth = dayjs().startOf('month').format('YYYY-MM-DD');
        const endOfMonth = dayjs().endOf('month').format('YYYY-MM-DD');
        this.queryInfo.checkdate = [startOfMonth, endOfMonth];
        const factoryid = Decrypt(getParams('factoryid'));
        const factoryInfo = await getFactoryInfo({ factoryid, isMapping: '是' });
        const { factoryname } = factoryInfo[0];
        this.queryInfo.factoryname = factoryname;
        this.queryInfo.factoryid = factoryid;
        document.title = factoryname + '发动机工时统计报表';
      }
    })
  </script>
</body>

</html>